package com.qymerge.manage.core.third.wechat.service.menu;

import com.qymerge.manage.core.third.wechat.config.WechatCommonConfig;
import com.qymerge.manage.core.third.wechat.dto.menu.MenuButtonDTO;
import com.qymerge.manage.core.third.wechat.dto.menu.MenuDTO;
import com.qymerge.manage.core.third.wechat.dto.menu.MenuResponseDTO;
import com.qymerge.manage.core.utils.HttpRequestUtil;
import com.qymerge.manage.core.utils.WxGsonUtil;
import com.google.gson.reflect.TypeToken;

import java.text.MessageFormat;

/**
 * Desc 公众号菜单
 * User licong
 * Date 2016/6/23
 * Time 16:10
 */
public class WechatMenuService {

    /**
     * 查询菜单
     *
     * @param accessToken
     * @return
     */
    public static MenuDTO getMenu(String accessToken) {
        String url = MessageFormat.format(WechatCommonConfig.url_menu_get, accessToken);
        String result = HttpRequestUtil.requestGet(url);
        MenuDTO menuDto = WxGsonUtil.json2T(result, new TypeToken<MenuDTO>() {
        }.getType());
        return menuDto;
    }

    /**
     * 创建菜单
     *
     * @param accessToken
     * @param menuButtonDto
     * @return
     */
    public static MenuResponseDTO createMenu(String accessToken, MenuButtonDTO menuButtonDto) {
        String url = MessageFormat.format(WechatCommonConfig.url_menu_create, accessToken);
        String result = HttpRequestUtil.requestPostBody(url, WxGsonUtil.toJson(menuButtonDto));
        MenuResponseDTO responseDto = WxGsonUtil.json2T(result, new TypeToken<MenuResponseDTO>() {
        }.getType());
        return responseDto;
    }

    /**
     * 删除菜单
     *
     * @param accessToken
     * @return
     */
    public static MenuResponseDTO deleteMenu(String accessToken) {
        String url = MessageFormat.format(WechatCommonConfig.url_menu_delete, accessToken);
        String result = HttpRequestUtil.requestGet(url);
        MenuResponseDTO responseDto = WxGsonUtil.json2T(result, new TypeToken<MenuResponseDTO>() {
        }.getType());
        return responseDto;
    }

    public static void main(String[] args) {
        String menu = "{\"button\":[{\"name\":\"活动\",\"sub_button\":[{\"type\":\"click\",\"name\":\"优惠活动\",\"key\":\"activity\",\"sub_button\":[]},{\"type\":\"view\",\"name\":\"美容知识\",\"url\":\"http:\\/\\/mp.weixin.qq.com\\/mp\\/getmasssendmsg?__biz=MzI2MTE2NDI0NQ==&from=1#wechat_webview_type=1&wechat_redirect\",\"sub_button\":[]},{\"type\":\"view\",\"name\":\"领取红包\",\"url\":\"http:\\/\\/wx.zsmy.cn\\/weixin\\/user\\/to_pre_registration.do\",\"sub_button\":[]}]},{\"name\":\"推荐\",\"sub_button\":[{\"type\":\"click\",\"name\":\"医生推荐\",\"key\":\"doctor\",\"sub_button\":[]},{\"type\":\"click\",\"name\":\"名医推荐\",\"key\":\"famous\",\"sub_button\":[]}]},{\"name\":\"更多\",\"sub_button\":[{\"type\":\"click\",\"name\":\"合作洽谈\",\"key\":\"cooperate\",\"sub_button\":[]},{\"type\":\"view\",\"name\":\"意见反馈\",\"url\":\"http:\\/\\/wx.zsmy.cn\\/weixin\\/advice\\/add.do\",\"sub_button\":[]},{\"type\":\"view\",\"name\":\"下载医生端\",\"url\":\"http:\\/\\/a.app.qq.com\\/o\\/simple.jsp?pkgname=cn.zsmy.akm.doctor\",\"sub_button\":[]},{\"type\":\"view\",\"name\":\"下载用户端\",\"url\":\"http:\\/\\/a.app.qq.com\\/o\\/simple.jsp?pkgname=cn.zsmy.user\",\"sub_button\":[]},{\"type\":\"view\",\"name\":\"关于我们\",\"url\":\"http:\\/\\/q.eqxiu.com\\/s\\/bnS0Q1p1\",\"sub_button\":[]}]}]}";

        String url = MessageFormat.format(WechatCommonConfig.url_menu_create, "MHzKgZ-b-AA2kFbkYBUnb0XQI0dIEKT4JlqKC_oQFrKdoc67aKSsyg88pWiJyVb_HZyJeRS2nvRWFYRGkbFI_rE-Vj03ce-bHZa-i1W8uxUFBHaAIAWSY");
        String result = HttpRequestUtil.requestPostBody(url, menu);
        System.out.println(result);
    }

}
